﻿using PLCS.Domain.Shared.Equipments;
using PLCS.Domain.Stations;

namespace PLCS.Domain.Equipments;

public class Equipment : BaseEntity
{
    /// <summary>
    /// 工厂编码
    /// </summary>
    public string? SiteNo { get; set; }

    /// <summary>
    /// 车间编码
    /// </summary>
    public string? AreaNo { get; set; }

    /// <summary>
    /// 产线编码
    /// </summary>
    public string? PdLineNo { get; set; }

    /// <summary>
    /// 设备名称
    /// </summary>
    public string EquName { get; set; }

    /// <summary>
    /// 设备名称MES
    /// </summary>
    public string EquName_Higher { get; set; }

    /// <summary>
    /// 设备名称PLC
    /// </summary>
    public string EquName_Lower { get; set; }

    /// <summary>
    /// 设备编号
    /// </summary>
    public string EquNum { get; init; }

    /// <summary>
    /// 在PLC中序号
    /// </summary>
    public int Plc_Index { get; set; }

    /// <summary>
    /// PLC地址
    /// </summary>
    public string PLCAddress { get; set; }

    /// <summary>
    /// 槽号
    /// </summary>
    public int Rack { get; set; }

    /// <summary>
    /// 机架号
    /// </summary>
    public int Slot { get; set; }

    /// <summary>
    /// DB块
    /// </summary>
    public int DbNumber { get; set; }

    /// <summary>
    /// 当前任务ID
    /// </summary>
    public int CurrentTaskId { get; set; }

    /// <summary>
    /// 当前工位
    /// </summary>
    public string? CurrentStation { get; private set; }

    /// <summary>
    /// 当前坐标
    /// </summary>
    public int CurrentPos { get; set; }

    /// <summary>
    /// 所属轨道
    /// </summary>
    public string? InTrack { get; set; }

    /// <summary>
    /// 最大坐标
    /// </summary>
    public int MaxPos { get; set; }

    /// <summary>
    /// 初始坐标
    /// </summary>
    public int InitPos { get; set; }

    /// <summary>
    /// 当前速度
    /// </summary>
    public int CurrentSpeed { get; set; }

    /// <summary>
    /// 当前托盘ID
    /// </summary>
    public string? TrayId { get; set; }

    /// <summary>
    /// 空闲状态
    /// </summary>
    public DeviceStatus Status { get; set; }

    /// <summary>
    /// 故障代码
    /// </summary>
    public int ErrCode { get; set; }

    /// <summary>
    /// 设备类型
    /// </summary>
    public EquipmentType EquType { get; set; }

    /// <summary>
    /// 包含工位
    /// </summary>
    public string ContainStationNos { get; set; }
    /// <summary>
    /// 当前列
    /// </summary>
    public string? CurrentColume { get; set; }

    /// <summary>
    /// 联机状态
    /// </summary>
    public OnlineMode OnlineStatus { get; set; }

    /// <summary>
    /// 是否使用
    /// </summary>
    public YesNo IsUse { get; set; } = YesNo.是;

    /// <summary>
    /// 设备锁定状态
    /// </summary>
    public YesNo IsLocked { get; set; }
    /// <summary>
    /// 完成信号
    /// </summary>

    public string CompleteSignal { get; set; } = "0";
    private Equipment()
    {
    }

    public Equipment(string equName, string equName_Higher, string equName_Lower, string equNum, int plc_Index, EquipmentType equType, string plcAddress, int dbNumber, int rack = 0, int slot = 1, string containStationNos = null, YesNo isUse = YesNo.是, string? siteNo = null, string? areaNo = null, string? pdLineNo = null)
    {
        EquName = equName;
        EquName_Lower = equName_Lower;
        EquName_Higher = equName_Higher;
        EquNum = equNum;
        Plc_Index = plc_Index;
        EquType = equType;
        ContainStationNos = containStationNos ?? equNum;
        IsUse = isUse;
        PLCAddress = plcAddress;
        DbNumber = dbNumber;
        Rack = rack;
        Slot = slot;
        SiteNo = siteNo;
        AreaNo = areaNo;
        PdLineNo = pdLineNo;
    }

    public void ChangeRGVStatus(DeviceStatus status)
    {
        if (status != Status)
            Status = status;
        AddLocalEvent(new RGVStatusChangeEvent()
        {
            SName = EquName,
            Status = status,
        });
    }

    public void ChangeCurrentStation(string stationName)
    {
        if (stationName != CurrentStation)
            CurrentStation = stationName;
        AddLocalEvent(new RGVCCurrentStationhangeEvent()
        {
            Name = EquName,
            StationName = stationName,
        });
    }
}